package com.shd.edanpei.order;

import com.shd.edanpei.base.data.RxBusCode;
import com.shd.edanpei.base.network.api.ApiCreator;
import com.shd.edanpei.base.network.model.NoDataResponse;
import com.shd.edanpei.base.network.rx.BaseObserver;
import com.shd.edanpei.base.network.rx.RxTransformer;
import com.shd.edanpei.base.rxbus.RxBus;
import com.shd.edanpei.bean.StatusBean;
import com.shd.edanpei.enums.StatusEnum;
import com.shd.edanpei.util.ToastUtil;

import io.reactivex.android.schedulers.AndroidSchedulers;

/**
 * Created by mc on 2018/11/1.
 * yang-wen-wen
 */

public class OrderApiService {

    /**
     * 发单者撤销订单
     *
     * @param undoReason   撤销理由
     * @param orderID      订单ID
     * @param orderType    订单类型
     * @param index        列表位置
     * @param isPayDeposit 是否支付押金
     */
    public static void userUndo(final String undoReason, final int orderID, final int orderType, final int index, final boolean isPayDeposit, final Listener listener) {
        ApiCreator.getApi().cancelOrder(orderID, orderType, undoReason)
                .compose(RxTransformer.<NoDataResponse>errorHandle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<NoDataResponse>() {
                    @Override
                    protected void onSuccess(NoDataResponse data) {
                        if (isPayDeposit) {
                            //已支付
                            RxBus.get().send(RxBusCode.REFRESH_ORDER_STATUS, new StatusBean(index, StatusEnum.waitRepay.getStatus()));
                        } else {
                            //未支付
                            RxBus.get().send(RxBusCode.REFRESH_ORDER_STATUS, new StatusBean(index, StatusEnum.cancel.getStatus()));
                        }
                        if (isPayDeposit) {
                            if(orderType==1||orderType==2){
                                //ToastUtil.showCenterToast("撤销成功！押金会在1-7个工作日退回原账户");
                            }else{
                                ToastUtil.showCenterToast("撤销成功！");
                            }

                        } else {
                            ToastUtil.showCenterToast("撤销成功！");
                        }

                    }

                    @Override
                    public void onFinish() {
                        listener.onFinish();
                    }
                });
    }


    /**
     * 接单者撤销订单
     *
     * @param undoReason 撤销理由
     * @param orderID    订单ID
     * @param orderType  订单类型
     * @param index      列表位置
     * @param listener
     */
    public static void bankUndo(final String undoReason, final int orderID, final int orderType, final int index, final Listener listener) {
        ApiCreator.getApi().refuseOrder(orderID, orderType, undoReason)
                .compose(RxTransformer.<NoDataResponse>errorHandle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<NoDataResponse>() {
                    @Override
                    protected void onSuccess(NoDataResponse data) {
                        RxBus.get().send(RxBusCode.REFRESH_ORDER_STATUS, new StatusBean(index, StatusEnum.refuse.getStatus()));
                        ToastUtil.showCenterToast("撤销成功！");
                    }

                    @Override
                    public void onFinish() {
                        listener.onFinish();
                    }
                });
    }


    public interface Listener {

        void onFinish();
    }
}
